package com.sskj.ffex.v.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sskj.ffex.MyAppLication;
import com.sskj.ffex.R;
import com.sskj.ffex.common.App;
import com.sskj.ffex.common.Constants;
import com.sskj.ffex.common.adapter.BaseAdapter;
import com.sskj.ffex.common.adapter.ViewHolder;
import com.sskj.ffex.common.widget.ToolBarLayout;
import com.sskj.ffex.m.bean.AddressList;
import com.sskj.ffex.p.activity.CashPresenter;
import com.sskj.ffex.utils.ClickUtil;
import com.sskj.ffex.v.base.BaseActivity;

import butterknife.BindView;

/**
 * <pre>
 *     author : 李岩
 *     e-mail : 465357793@qq.com
 *     time   : 2018/04/03
 *     desc   : 选择提现地址
 *     version: 1.0
 * </pre>
 */
public class CashAddressListActivity extends BaseActivity<CashPresenter> {

    @BindView(R.id.toolBar)
    ToolBarLayout mToolBar;
    @BindView(R.id.title_usdt)
    ImageView mTitleUSDT;
    @BindView(R.id.title_btc)
    ImageView mTitleBtc;
    @BindView(R.id.title_eth)
    ImageView mTitleEth;
    @BindView(R.id.address_recyclerveiw_btc)
    RecyclerView mAddressRecyclerveiwBtc;
    @BindView(R.id.address_recyclerveiw_usdt)
    RecyclerView mAddressRecyclerveiwUSDT;
    @BindView(R.id.address_recyclerveiw_eth)
    RecyclerView mAddressRecyclerveiwEth;
    @BindView(R.id.title)
    TextView title;
    private BaseAdapter<AddressList.EthBean> mBtcAdapter;
    private BaseAdapter<AddressList.EthBean> mEthAdapter;
    private BaseAdapter<AddressList.EthBean> mUSDTAdapter;
    private boolean mIsCallBack;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cash_address_list;
    }


    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.requestAddressList(MyAppLication.getAccount());
    }

    @Override
    public CashPresenter getPresenter() {
        return new CashPresenter();
    }

    @Override
    protected void initView() {
        mIsCallBack = getIntent().getBooleanExtra(Constants.INTENT_CALL_BACK, false);
        mToolBar.setLeftButtonOnClickLinster(v -> finish());
        mToolBar.setTitle(App.INSTANCE.getString(R.string.strCashAddressListActivity200));
        title.setVisibility(View.GONE);

        ClickUtil.click(mTitleUSDT, () -> {
            startActivity(new Intent(mActivity, CashAddressAddActivity.class).putExtra(Constants.INTENT_ADDRESS_TYPE, "usdt"));
        });
        ClickUtil.click(mTitleBtc, () -> {
            startActivity(new Intent(mActivity, CashAddressAddActivity.class).putExtra(Constants.INTENT_ADDRESS_TYPE, "btc"));
        });
        ClickUtil.click(mTitleEth, () -> {
            startActivity(new Intent(mActivity, CashAddressAddActivity.class).putExtra(Constants.INTENT_ADDRESS_TYPE, "eth"));
        });
        mAddressRecyclerveiwBtc.setLayoutManager(new LinearLayoutManager(this));
        mAddressRecyclerveiwEth.setLayoutManager(new LinearLayoutManager(this));
        mAddressRecyclerveiwUSDT.setLayoutManager(new LinearLayoutManager(this));


        /**
         * 比特币提币 地址列表
         */
        mBtcAdapter = new BaseAdapter<AddressList.EthBean>(R.layout.item_cash_address, null, mAddressRecyclerveiwBtc) {

            @Override
            public void bind(ViewHolder holder, AddressList.EthBean item) {
                holder.setText(R.id.address_list_remark, TextUtils.isEmpty(item.getNotes()) ? App.INSTANCE.getString(R.string.strCashAddressListActivity203) : item.getNotes());
                holder.setText(R.id.address_list_content, item.getQiaobao_url());
                holder.setOnClickListener(R.id.item_address_layout, v -> {
                    if (mIsCallBack) {
                        Intent intent = new Intent();
                        intent.putExtra("address_url", item.getQiaobao_url());
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                });
                holder.setOnClickListener(R.id.address_list_content_delete, v -> {
                    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                    builder.setTitle(App.INSTANCE.getString(R.string.strCashAddressListActivity205))
                            .setMessage(App.INSTANCE.getString(R.string.strCashAddressListActivity206))
                            .setPositiveButton(App.INSTANCE.getString(R.string.strCashAddressListActivity207), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                    mPresenter.requestDelite(1, holder.getLayoutPosition(), item.getId());
                                }
                            })
                            .setNegativeButton(App.INSTANCE.getString(R.string.strCashAddressListActivity208), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            });
                    builder.show();
                });
            }
        };
        /**
         * 比特币提币 地址列表
         */
        mEthAdapter = new BaseAdapter<AddressList.EthBean>(R.layout.item_cash_address, null, mAddressRecyclerveiwEth) {
            @Override
            public void bind(ViewHolder holder, AddressList.EthBean item) {
                holder.setText(R.id.address_list_remark, TextUtils.isEmpty(item.getNotes()) ? App.INSTANCE.getString(R.string.strCashAddressListActivity203) : item.getNotes());
                holder.setText(R.id.address_list_content, item.getQiaobao_url());
                holder.setOnClickListener(R.id.item_address_layout, v -> {
                    if (mIsCallBack) {
                        Intent intent = new Intent();
                        intent.putExtra("address_url", item.getQiaobao_url());
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                });
                holder.setOnClickListener(R.id.address_list_content_delete, v -> {
                    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                    builder.setTitle(App.INSTANCE.getString(R.string.strCashAddressListActivity205))
                            .setMessage(App.INSTANCE.getString(R.string.strCashAddressListActivity206))
                            .setPositiveButton(App.INSTANCE.getString(R.string.strCashAddressListActivity207), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                    mPresenter.requestDelite(0, holder.getLayoutPosition(), item.getId());
                                }
                            })
                            .setNegativeButton(App.INSTANCE.getString(R.string.strCashAddressListActivity208), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            });
                    builder.show();
                });
            }
        };


    }


    public void onResponseCashAddressListSuccess(AddressList data) {
        mBtcAdapter.setNewData(data.getBtc());
        mEthAdapter.setNewData(data.getEth());
    }

    /**
     * @param type     type 0:eth ;  1: btc
     * @param position
     */
    public void onDeliteSuccess(int type, int position) {
        if (type == 1) {//比特币删除
            mBtcAdapter.remove(position);
        } else {
            mEthAdapter.remove(position);
        }
    }
}
